
package com.wyzz.hr.action;

import java.io.File;
import java.util.*;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.IEmpEduHisBo;
import com.wyzz.hr.pojo.Emphistoryedu;
import com.wyzz.hr.spring.SpringBeanFactory;

public class EmpEduHisDWR extends BaseAction {

	public EmpEduHisDWR() {
	}

	public String eduHisAdd() throws Exception {
		if ("self".equals(emphistoryedu.getEmployee().getId()))
			emphistoryedu.setEmployee(getCurrentEmp());
		if ("SUB".equals(super.authorityCondition)
				&& !checkAuth(emphistoryedu.getEmployee().getId())) {
			addActionError("\u60A8\u6CA1\u6709\u589E\u52A0\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");// 您没有增加权限执行本操作
			// ！
			return "error";
		}
		empEduHisBo = (IEmpEduHisBo) SpringBeanFactory.getBean("empEduHisBo");
		if (fileFileName != null && !"".equals(fileFileName)) {
			String UploadResult;
			String pathConfig = "sys.profile.file.path";
			String typeConfig = "sys.profile.file.type";
			String lengthConfig = "sys.profile.file.length";
			String postfix = fileFileName.substring(fileFileName
					.lastIndexOf("."));
			fileFileName = (new StringBuilder()).append(UUID.randomUUID())
					.append(postfix).toString();
			UploadResult = FileOperate.buildFile(file, pathConfig, typeConfig,
					lengthConfig, new String[] { fileFileName });
			if ("property".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u4E0A\u4F20\u9519\u8BEF\uFF01");// 文件的上传错误
				// ！
				return "error";
			}
			if ("fileExtendNameError".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u540E\u7F00\u540D\u4E0D\u5408\u6CD5\uFF01");
				return "error";
			}
			try {
				emphistoryedu.setEheAttachment(fileFileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		emphistoryedu.setEmployee(emphistoryedu.getEmployee());
		emphistoryedu.setEheCreateBy(getCurrentEmpNo());
		emphistoryedu.setEheCreateDate(new Date());
		emphistoryedu.setEheLastChangeBy(getCurrentEmpNo());
		emphistoryedu.setEheLastChangeTime(new Date());
		empEduHisBo.insert(emphistoryedu);
		addActionMessage("\u6559\u80B2\u80CC\u666F\u589E\u52A0\u6210\u529F\u3002");// 教育背景增加成功
		// 。
		return "success";
	}

	public String eduHisDel(String ehjId) {
		String auth = DWRUtil.checkAuth("empeduhisdwr", "");
		if ("error".equalsIgnoreCase(auth))
			return "noauth";
		empEduHisBo = (IEmpEduHisBo) SpringBeanFactory.getBean("empEduHisBo");
		Emphistoryedu eduSearch = new Emphistoryedu();
		eduSearch.setEheId(ehjId);
		List list = empEduHisBo.search(eduSearch);
		Emphistoryedu empHE = (Emphistoryedu) list.get(0);
		if ("SUB".equals(auth) && !checkAuth(empHE.getEmployee().getId())) {
			return "noauth";
		} else {
			empEduHisBo.deleteAttach(ehjId, empHE.getEheAttachment());
			empEduHisBo.delete(ehjId);
			return "success";
		}
	}

	public String eduHisUpdate() throws Exception {
		Emphistoryedu empOldHE;
		empEduHisBo = (IEmpEduHisBo) SpringBeanFactory.getBean("empEduHisBo");
		List list = empEduHisBo.search(emphistoryedu);
		if (list.size() == 0)
			return "error";
		empOldHE = (Emphistoryedu) list.get(0);
		if ("SUB".equals(super.authorityCondition)
				&& !checkAuth(empOldHE.getEmployee().getId())) {
			addActionError("\u60A8\u6CA1\u6709\u4FEE\u6539\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		if (fileFileName != null && !"".equals(fileFileName)) {
			String UploadResult;
			String pathConfig = "sys.profile.file.path";
			String typeConfig = "sys.profile.file.type";
			String lengthConfig = "sys.profile.file.length";
			if (!StringUtils.isEmpty(empOldHE.getEheAttachment()))
				FileOperate.deleteFile(pathConfig, empOldHE.getEheAttachment());
			String postfix = fileFileName.substring(fileFileName
					.lastIndexOf("."));
			fileFileName = (new StringBuilder()).append(UUID.randomUUID())
					.append(postfix).toString();
			UploadResult = FileOperate.buildFile(file, pathConfig, typeConfig,
					lengthConfig, new String[] { fileFileName,
							empOldHE.getEheAttachment() });
			if ("property".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u4E0A\u4F20\u9519\u8BEF\uFF01");
				return "error";
			}
			if ("fileExtendNameError".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u540E\u7F00\u540D\u4E0D\u5408\u6CD5\uFF01");
				return "error";
			}
			try {
				empOldHE.setEheAttachment(fileFileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		empOldHE.setEheDateStart(emphistoryedu.getEheDateStart());
		empOldHE.setEheDateEnd(emphistoryedu.getEheDateEnd());
		empOldHE.setEheSchool(emphistoryedu.getEheSchool());
		empOldHE.setEheMajor(emphistoryedu.getEheMajor());
		empOldHE.setEheDegree(emphistoryedu.getEheDegree());
		empOldHE.setEheComments(emphistoryedu.getEheComments());
		emphistoryedu.setEheLastChangeBy(getCurrentEmpNo());
		emphistoryedu.setEheLastChangeTime(new Date());
		empEduHisBo.update(empOldHE);
		addSuccessInfo("\u4FEE\u6539\u5458\u5DE5\u6559\u80B2\u80CC\u666F\u6210\u529F\u3002");
		return "success";
	}

	public String attachDelete() throws Exception {
		if (eheId == null || eheId.equals("") || fileFileName == null
				|| "".equals(fileFileName)) {
			addActionError("\u53C2\u6570\u4F20\u9012\u9519\u8BEF\uFF01");
			return "error";
		}
		empEduHisBo = (IEmpEduHisBo) SpringBeanFactory.getBean("empEduHisBo");
		String fetches[] = { Emphistoryedu.PROP_EMPLOYEE };
		Emphistoryedu empOldHE = empEduHisBo.load(eheId, fetches);
		if ("SUB".equals(super.authorityCondition)
				&& !checkAuth(empOldHE.getEmployee().getId())) {
			addActionError("\u60A8\u6CA1\u6709\u5220\u9664\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		if (!empEduHisBo.deleteAttach(eheId, fileFileName)) {
			addActionError("\u9644\u4EF6\u5220\u9664\u5931\u8D25\uFF01");
			return "error";
		} else {
			addActionMessage("\u9644\u4EF6\u5220\u9664\u6210\u529F\u3002");
			return "success";
		}
	}

	public Emphistoryedu loadEduHis(String id) {
		if (StringUtils.isEmpty(id)) {
			return null;
		} else {
			id = id.trim();
			empEduHisBo = (IEmpEduHisBo) SpringBeanFactory
					.getBean("empEduHisBo");
			Emphistoryedu result = empEduHisBo.load(id, new String[0]);
			return result;
		}
	}

	public Emphistoryedu getEmphistoryedu() {
		return emphistoryedu;
	}

	public void setEmphistoryedu(Emphistoryedu emphistoryedu) {
		this.emphistoryedu = emphistoryedu;
	}

	public String getEheId() {
		return eheId;
	}

	public void setEheId(String eheId) {
		this.eheId = eheId;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getEmpNo() {
		return empNo;
	}

	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}

	private static final long serialVersionUID = 1L;
	private Emphistoryedu emphistoryedu;
	private IEmpEduHisBo empEduHisBo;
	private String eheId;
	private File file;
	private String fileFileName;
	private String empNo;
}
